[bits 32]

; external symbols
extern _handle_irq
extern _handle_divide_error
extern _handle_invalid_instruction
extern _handle_gpf
extern _handle_page_fault
extern _handle_sched

; exported symbols
global _default_intr_hdr
global _divide_error_trap_hdr
global _invalid_instruction_trap_hdr
global _gpf_trap_hdr
global _page_fault_trap_hdr

; exported data symbols

section .data

; end of section .data

section .text

default_intr_entry:

	;
	; note that eax has already saved
	;
	
	pop		eax
	sub		eax, _default_intr_hdr
	shr		eax, 3							; eax = eax / 8 -> eax = intr number
	mov		[esp + 4], eax					; save intr number the position of "errcode" in the stack	
	mov		eax, _handle_irq
	align	4

	;
	; save all registers
	;

common_interrupt:	

	push	es
	push	ds
	push	fs
	push	gs
	push	ebp
	push	edi
	push	esi
	push	edx
	push	ecx
	push	ebx
	
	call	eax								; call the actual handler
	
	;
	; check if we need resched
	;

	mov		eax, _handle_sched
	call	eax	

	;
	; restore all registers
	;

restore_all:

	pop		ebx
	pop		ecx
	pop		edx
	pop		esi
	pop		edi
	pop		ebp
	pop		gs
	pop		fs
	pop		ds
	pop		es
	pop		eax
	add		esp,4
	iret

_divide_error_trap_hdr:

	db		0x68							; push dword 0
	dd		0								; no error code for divide error
	push	eax								; save eax
	mov		eax, _handle_divide_error
	jmp		common_interrupt

_invalid_instruction_trap_hdr:
_gpf_trap_hdr:

_page_fault_trap_hdr:

	;
	; page fault exception already carry an error code
	
	push	eax								; save eax
	mov		eax, _handle_page_fault
	jmp		common_interrupt

	align	16
_default_intr_hdr:

	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 0
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 1
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 2
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 3
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 4
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 5
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 6
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 7
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 8
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 9
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 10
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 11
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 12
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 13
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 14
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 15
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 16
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 17
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 18
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 19
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 20
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 21
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 22
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 23
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 24
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 25
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 26
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 27
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 28
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 29
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 30
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 31
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 32
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 33
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 34
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 35
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 36
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 37
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 38
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 39
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 40
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 41
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 42
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 43
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 44
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 45
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 46
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 47
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 48
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 49
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 50
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 51
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 52
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 53
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 54
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 55
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 56
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 57
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 58
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 59
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 60
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 61
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 62
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 63
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 64
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 65
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 66
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 67
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 68
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 69
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 70
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 71
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 72
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 73
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 74
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 75
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 76
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 77
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 78
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 79
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 80
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 81
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 82
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 83
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 84
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 85
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 86
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 87
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 88
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 89
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 90
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 91
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 92
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 93
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 94
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 95
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 96
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 97
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 98
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 99
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 100
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 101
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 102
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 103
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 104
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 105
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 106
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 107
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 108
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 109
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 110
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 111
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 112
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 113
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 114
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 115
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 116
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 117
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 118
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 119
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 120
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 121
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 122
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 123
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 124
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 125
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 126
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 127
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 128
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 129
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 130
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 131
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 132
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 133
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 134
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 135
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 136
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 137
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 138
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 139
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 140
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 141
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 142
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 143
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 144
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 145
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 146
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 147
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 148
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 149
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 150
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 151
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 152
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 153
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 154
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 155
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 156
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 157
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 158
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 159
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 160
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 161
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 162
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 163
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 164
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 165
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 166
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 167
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 168
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 169
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 170
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 171
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 172
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 173
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 174
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 175
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 176
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 177
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 178
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 179
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 180
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 181
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 182
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 183
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 184
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 185
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 186
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 187
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 188
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 189
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 190
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 191
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 192
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 193
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 194
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 195
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 196
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 197
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 198
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 199
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 200
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 201
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 202
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 203
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 204
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 205
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 206
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 207
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 208
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 209
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 210
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 211
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 212
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 213
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 214
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 215
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 216
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 217
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 218
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 219
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 220
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 221
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 222
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 223
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 224
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 225
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 226
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 227
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 228
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 229
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 230
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 231
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 232
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 233
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 234
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 235
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 236
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 237
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 238
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 239
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 240
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 241
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 242
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 243
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 244
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 245
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 246
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 247
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 248
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 249
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 250
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 251
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 252
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 253
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 254
	align	4
	push	eax
	push	eax
	call	default_intr_entry				; entry for intr 255
	align	4

; end of section .text
